package com.wsoft.enums;

import com.alibaba.fastjson.JSONObject;
import com.wsoft.dto.CodeRulesDto;
import com.wsoft.utils.CodeRulesUtil;
import lombok.Getter;
import org.springframework.lang.Nullable;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author z001
 * @Date 2024/10/16 14:14
 */
@Getter
public enum RulesTypeEnum {
    /**
     * 常量
     */
    CONSTANT(1, "常量"),

    COVER_CONSTANT(2, "补位常量"),
    /**
     * 组织编码
     */
    ORG_CODE(3, "组织编码"),
    /**
     * 用户编码
     */
    USER_CODE(4, "用户编码"),
    /**
     * 流水号
     */
    SERIAL_NUMBER(5, "流水号"),
    /**
     * 随机码
     */
    RANDOM_CODE(6, "随机码"),
    /**
     * 系统时间
     */
    SYSTEM_DATE(7, "系统时间"),
    /**
     * 页面元素
     */
    PAGE_ELEMENTS(8, "页面元素");

    private final Integer type;
    private final String desc;

    private static final Map<Integer, RulesTypeEnum> MAPPING = new HashMap(16);

    static {
        RulesTypeEnum[] var0 = values();
        for (RulesTypeEnum typeEnum : var0) {
            MAPPING.put(typeEnum.getType(), typeEnum);
        }
    }

    RulesTypeEnum(Integer type, String desc) {
        this.type = type;
        this.desc = desc;
    }

    @NotNull
    public static RulesTypeEnum resolve(@Nullable Integer type) {
        if (type != null) {
            return MAPPING.get(type);
        }
        return null;
    }


    /**
     * 获取code
     * @param dto
     * @param templateIndex
     * @param objectJson
     * @return
     */
    public String getCode(CodeRulesDto dto, List<Integer> templateIndex, JSONObject objectJson) {
        switch (this) {
            case CONSTANT:
                return  CodeRulesUtil.buildConstant(dto);
            case COVER_CONSTANT:
                return CodeRulesUtil.buildCoverConstant();
            case ORG_CODE:
                return CodeRulesUtil.buildOrgCode(dto,templateIndex);
            case USER_CODE:
                return CodeRulesUtil.buildUserCode(dto,templateIndex);
            case SERIAL_NUMBER:
                return CodeRulesUtil.buildSerialNumber();
            case RANDOM_CODE:
                return CodeRulesUtil.buildRandomCode(dto);
            case SYSTEM_DATE:
                return CodeRulesUtil.buildSystemDate(dto,templateIndex);
            case PAGE_ELEMENTS:
                return CodeRulesUtil.buildPageElements(dto,objectJson,templateIndex);
            default:
                return null;
        }
    }


}
